package com.wyh.service.serviceImpl.gfiveImpl.tree_service_impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.domain.dto.gfive.LoginUser;
import com.wyh.domain.vo.gfive.treeVo.OrganizationVO;
import com.wyh.mapper.gfivemapper.tree_mapper.OrganizationVOMapper;
import com.wyh.service.gfive.tree_service.IOrganizationVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrganizationVOServiceImpl extends ServiceImpl<OrganizationVOMapper, OrganizationVO>
  implements IOrganizationVOService {
    @Autowired
    private OrganizationVOMapper mapper;
    @Override
    public List<OrganizationVO> findAll() {
        return mapper.selectAll();
    }

    @Override
    public OrganizationVO findRoot() {
        UsernamePasswordAuthenticationToken authentication =
                (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        Integer organizationid = loginUser.getUser().getOrganizationid();
        Integer userid = loginUser.getUser().getId();
        QueryWrapper<OrganizationVO> wrapper = new QueryWrapper<>();
        wrapper.eq("id",organizationid);
        OrganizationVO organizationVO = mapper.selectList(wrapper).get(0);
        return organizationVO;
    }

    @Override
    public List<OrganizationVO> findList(Integer id) {
        QueryWrapper<OrganizationVO> wrapper = new QueryWrapper<>();
        wrapper.eq("superiorid",id);
        List<OrganizationVO> organizationVOS = mapper.selectList(wrapper);
        return organizationVOS;
    }
}
